import { Body, Controller, Get, Post, Query } from "@nestjs/common";
import { CreateLog, Roles } from "@/common/decorators";
import { Role } from "@/common/enum/role.enum";
import { RequiredPipe } from "@/common/pipes/required.pipe";
import { SysRole } from "@/database/entity/SysRole";
import { CreateRoleDto } from "./dto/create-role.dto";
import { PageRoleDto } from "./dto/page-role.dto";
import { UpdateRoleDto } from "./dto/update-role.dto";
import { UpdateRoleAuthDto } from "./dto/update-role-auth.dto";
import { RoleService } from "./role.service";

@Controller("sys/role")
export class RoleController {
    constructor(private readonly roleService: RoleService) {}

    /** 新增角色 */
    @CreateLog({ module: "系统角色", type: "create", action: "创建系统角色" }, SysRole)
    @Roles(Role.SUPER_ADMIN)
    @Post("create")
    async create(@Body() role: CreateRoleDto) {
        return this.roleService.create(role);
    }

    /** 删除角色 */
    @CreateLog({ module: "系统角色", type: "delete", action: "删除系统角色" }, SysRole)
    @Roles(Role.SUPER_ADMIN)
    @Post("remove")
    async remove(@Body("id") id: string) {
        return this.roleService.remove(id);
    }

    /** 更新角色 */
    @CreateLog({ module: "系统角色", type: "update", action: "更新系统角色" }, SysRole)
    @Roles(Role.SUPER_ADMIN)
    @Post("update")
    async update(@Body() role: UpdateRoleDto) {
        return this.roleService.update(role);
    }

    /** 查看详情 */
    @Get("findById")
    async findById(@Query("id", RequiredPipe) id: string) {
        return this.roleService.findById(id);
    }

    /** 更新角色的菜单与权限 */
    @CreateLog({ module: "系统角色", type: "update", action: "更新角色权限" }, SysRole)
    @Post("updateRolePermissions")
    async updateRolePermissions(@Body() data: UpdateRoleAuthDto) {
        return this.roleService.updateRolePermissions(data);
    }

    /** 获取角色分页列表 */
    @Post("page")
    async page(@Body() data: PageRoleDto) {
        return this.roleService.page(data);
    }

    /** 获取角色的菜单和权限的ID集合 */
    @Get("findRolePermissions")
    async findRolePermissions(@Query("roleId") roleId: string) {
        return this.roleService.findRolePermissions(roleId);
    }
}
